<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Template
{
    /**
     * Plugin identification string
     *
     * @var string
     */
    private $_plugin;

    /**
     * Template identification string
     *
     * @var string
     */
    private $_template;

    /**
     * Template parameters
     *
     * @var array
     */
    public $params = array();

    /**
     * Template constructor
     *
     * @param object $pid
     * @param string $template
     * @return void
     */
    public function __construct($pid, $template)
    {
        // is the $pid variable instance of PluginAPI class/interface?
        if ($pid instanceof PluginAPI) {
        	$this->_plugin = get_class($pid);
        	$this->_plugin = substr($this->_plugin, strlen(cfg::PLUGIN_PREFIX));
        	$this->_plugin = strtolower($this->_plugin);
        } else {
            throw new Sigma_Exception('The parameter must be an instance of the PluginAPI class.');
        }

        // set template ID
        $this->_template = (string) $template;
    }

    /**
     * Analyze template
     *
     * @return void
     */
    public function analyze()
    {
    	// templatet caching
    	static $tplCache = array();

        // is Plugin ID in the cache?
        if (!$this->_plugin) {
            throw new Sigma_Exception('The plugin ID must be set before template can be parsed.');
        }

        // is the template stored in the cache?
		if (!array_key_exists($this->_template, (array) $tplCache[$this->_plugin])) {
	        // load template codes from the database
	        $sql = db::query("SELECT `template_id`, `content` FROM %t", "templates", "WHERE `plugin_id`='%s'", $this->_plugin);
	        while ($row = db::fetchObject($sql)) {
	        	$tplCache[$this->_plugin][$row->template_id] = (string) $row->content;
	        }
	        db::free($sql);
        }

		// analyze template code
		Parser::singleton()->setTemplate($this);
		Parser::singleton()->analyze($tplCache[$this->_plugin][$this->_template]);
		Parser::singleton()->unsetTemplate();
    }

    /**
     * Create a new parameter
     *
     * @param string $key
     * @param mixed $value
     * @return void
     */
    public function setParam($key, $value)
    {
        if (!array_key_exists($key, $this->params)) {
        	$this->params[$key] = $value;
        }
    }

    /**
     * Return required parameter with specified key
     *
     * @param string $key
     * @return string
     */
    public function getParam($key)
    {
        if (array_key_exists($key, $this->params)) {
        	return $this->params[$key];
        }
        return NULL;
    }
}
